package com.u3k.app.mid.utils.common;

import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.os.Looper;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import com.u3k.app.mid.MiddleMain;
import com.u3k.app.mid.utils.statistics.StatisticsManager;
import com.u3k.mid.*;
import com.u3k.mid.LogUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import org.json.JSONArray;

public class Util {
    private static final String KEY_APP_ID = "mid_app_id";
    public static final String KEY_BUNDLE_ID = "mid_bundle_id";
    private static final String KEY_CHANNEL_ID = "mid_channel_id";
    public static final String KEY_SHELL_VERSION = "mid_shell_version";
    private static final String KEY_USER_IMEI = "mid_user_imei";
    private static final String KEY_USER_TOKEN = "mid_user_token";
    static String TAG = MiddleMain.class.getSimpleName();
    private static final int kSystemRootStateDisable = 0;
    private static final int kSystemRootStateEnable = 1;
    private static final int kSystemRootStateUnknow = -1;
    private static int systemRootState = kSystemRootStateUnknow;

    public static boolean checkRegistered(Context context) {
        CharSequence userToken = getUserToken(context);
        LogUtils.LogError(TAG, "checkRegister   token=" + userToken);
        return !TextUtils.isEmpty(userToken);
    }

    public static int copyFile(String str, String str2) {
        try {
            InputStream fileInputStream = new FileInputStream(str);
            OutputStream fileOutputStream = new FileOutputStream(str2);
            byte[] bArr = new byte[1024];
            while (true) {
                int read = fileInputStream.read(bArr);
                if (read > 0) {
                    fileOutputStream.write(bArr, kSystemRootStateDisable, read);
                } else {
                    fileInputStream.close();
                    fileOutputStream.close();
                    return kSystemRootStateDisable;
                }
            }
        } catch (Exception e) {
            LogUtils.a("error", e);
            StatisticsManager.getInstance().logException(ae.a(), e);
            return kSystemRootStateUnknow;
        }
    }

    public static String getAndroidId(Context context) {
        try {
            return aw.a(context);
        } catch (Exception e) {
            LogUtils.a("error", e);
            StatisticsManager.getInstance().logException(ae.a(), e);
            return "";
        }
    }

    public static String getAppId(Context context) {
        String str;
        synchronized (KEY_APP_ID) {
            try {
                str = (String) bb.b(context, KEY_APP_ID, null);
            } catch (Throwable th) {
                while (true) {
                    String str2 = KEY_APP_ID;
                }
            }
        }
        LogUtils.LogError(TAG, "getAppId  appId=" + str);
        return str;
    }

    public static String getBundleId(Context context) {
        String str;
        synchronized (KEY_BUNDLE_ID) {
            try {
                str = (String) bb.b(context, KEY_BUNDLE_ID, "");
            } catch (Throwable th) {
                String str2 = KEY_BUNDLE_ID;
            }
        }
        return str;
    }

    public static String getChannelId(Context context) {
        String str;
        synchronized (KEY_CHANNEL_ID) {
            try {
                str = (String) bb.b(context, KEY_CHANNEL_ID, null);
            } catch (Throwable th) {
                while (true) {
                    String str2 = KEY_CHANNEL_ID;
                }
            }
        }
        LogUtils.LogError(TAG, "getChannelId  channelId=" + str);
        return str;
    }

    public static String getGoogleAdvertiseId(Context context) {
        try {
            return aw.b(context);
        } catch (Exception e) {
            LogUtils.a("error", e);
            StatisticsManager.getInstance().logException(ae.a(), e);
            return "";
        }
    }

    public static String getImei(Context context) {
        try {
            if (aq.c(context)) {
                return ((TelephonyManager) context.getSystemService("phone")).getDeviceId();
            }
        } catch (Exception e) {
            LogUtils.a("error", e);
            StatisticsManager.getInstance().logException(ae.a(), e);
        }
        return "";
    }

    public static String getImsi(Context context) {
        try {
            if (aq.c(context)) {
                return ((TelephonyManager) context.getSystemService("phone")).getSubscriberId();
            }
        } catch (Exception e) {
            LogUtils.a("error", e);
            StatisticsManager.getInstance().logException(ae.a(), e);
        }
        return "";
    }

    public static String getInstallDir(Context context) {
        return context.getApplicationContext().getPackageResourcePath();
    }

    public static double[] getLocation(Context context) {
        double latitude;
        Exception e;
        double d = 0.0d;
        double[] dArr = new double[]{d, d};
        String str = "network";
        try {
            LocationManager locationManager = (LocationManager) context.getSystemService("location");
            List providers = locationManager.getProviders(true);
            if (providers.contains("gps")) {
                str = "gps";
            } else if (providers.contains("network")) {
                str = "network";
            }
            boolean b = av.b(context, "android.permission.ACCESS_COARSE_LOCATION");
            boolean b2 = av.b(context, "android.permission.ACCESS_FINE_LOCATION");
            if (b || b2) {
                Location lastKnownLocation = locationManager.getLastKnownLocation(str);
                if (lastKnownLocation != null) {
                    latitude = lastKnownLocation.getLatitude();
                    try {
                        d = lastKnownLocation.getLongitude();
                    } catch (Exception e2) {
                        e = e2;
                        LogUtils.a("error", e);
                        dArr[kSystemRootStateDisable] = d;
                        dArr[kSystemRootStateEnable] = d;
                        StatisticsManager.getInstance().logException(ae.a(), e);
                        dArr[kSystemRootStateDisable] = latitude;
                        dArr[kSystemRootStateEnable] = d;
                        return dArr;
                    }
                    dArr[kSystemRootStateDisable] = latitude;
                    dArr[kSystemRootStateEnable] = d;
                    return dArr;
                }
            }
            latitude = d;
        } catch (Exception e3) {
            e = e3;
            latitude = d;
            LogUtils.a("error", e);
            dArr[kSystemRootStateDisable] = d;
            dArr[kSystemRootStateEnable] = d;
            StatisticsManager.getInstance().logException(ae.a(), e);
            dArr[kSystemRootStateDisable] = latitude;
            dArr[kSystemRootStateEnable] = d;
            return dArr;
        }
        dArr[kSystemRootStateDisable] = latitude;
        dArr[kSystemRootStateEnable] = d;
        return dArr;
    }

    public static String getLocationString(Context context) {
        double[] location = getLocation(context);
        return "" + location[kSystemRootStateDisable] + "x" + location[kSystemRootStateEnable];
    }

    public static String getMac(Context context) {
        try {
            return aw.l();
        } catch (Exception e) {
            LogUtils.a("error", e);
            StatisticsManager.getInstance().logException(ae.a(), e);
            return "";
        }
    }

    public static String getMiddleVersion(Context context) {
        return "mid1.1.0";
    }

    public static String getSelfPkgName(Context context) {
        return context.getPackageName();
    }

    public static String getServImei(Context context) {
        String str;
        synchronized (KEY_USER_IMEI) {
            try {
                str = (String) bb.b(context, KEY_USER_IMEI, null);
            } catch (Throwable th) {
                String str2 = KEY_USER_IMEI;
            }
        }
        return str;
    }

    public static String getShellVersion(Context context) {
        String str;
        synchronized (KEY_SHELL_VERSION) {
            try {
                str = (String) bb.b(context, KEY_SHELL_VERSION, "");
            } catch (Throwable th) {
                String str2 = KEY_SHELL_VERSION;
            }
        }
        return str;
    }

    public static JSONArray getUsedPermissionJson(Context context) {
        try {
            JSONArray jSONArray = new JSONArray();
            String[] a = av.a(context);
            if (a == null) {
                return jSONArray;
            }
            int length = a.length;
            for (int i = kSystemRootStateDisable; i < length; i += kSystemRootStateEnable) {
                jSONArray.put(a[i]);
            }
            return jSONArray;
        } catch (Exception e) {
            LogUtils.a("error", e);
            StatisticsManager.getInstance().logException(ae.a(), e);
            return null;
        }
    }

    public static String getUserToken(Context context) {
        String str;
        synchronized (KEY_USER_TOKEN) {
            try {
                str = (String) bb.b(context, KEY_USER_TOKEN, null);
            } catch (Throwable th) {
                String str2 = KEY_USER_TOKEN;
            }
        }
        return str;
    }

    public static boolean isMainThread() {
        return Looper.getMainLooper() == Looper.myLooper();
    }

    public static boolean isRootSystem() {
        if (systemRootState == kSystemRootStateEnable) {
            return true;
        }
        if (systemRootState == 0) {
            return false;
        }
        String[] strArr = new String[]{"/system/bin/", "/system/xbin/", "/system/sbin/", "/sbin/", "/vendor/bin/"};
        int i = kSystemRootStateDisable;
        while (i < strArr.length) {
            try {
                File file = new File(strArr[i] + "su");
                if (file == null || !file.exists()) {
                    i += kSystemRootStateEnable;
                } else {
                    systemRootState = kSystemRootStateEnable;
                    return true;
                }
            } catch (Exception e) {
                LogUtils.a("error", e);
                StatisticsManager.getInstance().logException(ae.a(), e);
            }
        }
        systemRootState = kSystemRootStateDisable;
        return false;
    }

    public static void saveAppId(Context context, String str) {
        synchronized (KEY_APP_ID) {
            try {
                bb.a(context, KEY_APP_ID, str);
                LogUtils.LogError(TAG, "saveAppId  appId=" + str);
            } catch (Throwable th) {
                String str2 = KEY_APP_ID;
            }
        }
    }

    public static void saveBundleId(Context context, String str) {
        synchronized (KEY_BUNDLE_ID) {
            try {
                bb.a(context, KEY_BUNDLE_ID, str);
                LogUtils.LogError(TAG, "saveBundleId  bundleId=" + str);
            } catch (Throwable th) {
                String str2 = KEY_BUNDLE_ID;
            }
        }
    }

    public static void saveChannelId(Context context, String str) {
        synchronized (KEY_CHANNEL_ID) {
            try {
                bb.a(context, KEY_CHANNEL_ID, str);
                LogUtils.LogError(TAG, "saveChannelId  channelid=" + str);
            } catch (Throwable th) {
                String str2 = KEY_CHANNEL_ID;
            }
        }
    }

    public static void saveServImei(Context context, String str) {
        synchronized (KEY_USER_IMEI) {
            try {
                bb.a(context, KEY_USER_IMEI, str);
            } catch (Throwable th) {
                String str2 = KEY_USER_IMEI;
            }
        }
    }

    public static void saveShellVersion(Context context, String str) {
        synchronized (KEY_SHELL_VERSION) {
            try {
                bb.a(context, KEY_SHELL_VERSION, str);
                LogUtils.LogError(TAG, "saveShellVersion  shellVersion=" + str);
            } catch (Throwable th) {
                String str2 = KEY_SHELL_VERSION;
            }
        }
    }

    public static void saveUserToken(Context context, String str) {
        synchronized (KEY_USER_TOKEN) {
            try {
                bb.a(context, KEY_USER_TOKEN, str);
            } catch (Throwable th) {
                String str2 = KEY_USER_TOKEN;
            }
        }
    }
}
